import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    //盛水最多的容器
    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length-1;
        int maxArea = Integer.MIN_VALUE;
        while (left < right) {
            int multiplication = Math.min(height[right],height[left]) * (right - left);
            maxArea = Math.max(multiplication,maxArea);
            if(height[left] > height[right]) {
                right--;
            }else {
                left++;
            }
        }
        return maxArea;
    }
    //三数之和
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList();
        Arrays.sort(nums);
        int n = nums.length;
        if(n < 3) {
            return list;
        }
        for(int i= 0; i < n;){
            twoSum(nums,i,list);
            int tmp = nums[i];
            //去掉传入相同数字
            while( i < nums.length && nums[i] == tmp) {
                i++;
            }
        }
        return list;
    }
    private void twoSum(int[] nums, int i, List<List<Integer>> list) {
        int left = i + 1;
        int right = nums.length-1;
        while(left < right) {
            if(nums[left] + nums[right] < -nums[i]) {
                left++;
            } else if(nums[left] + nums[right] > -nums[i]) {
                right--;
            } else{
                list.add(Arrays.asList(nums[i],nums[left],nums[right]));
                int tmp = nums[left];
                //去掉重复
                while(left < right && nums[left] == tmp) {
                    left++;
                }
            }
        }
    }
}
